<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
</body>

</html>
<script>
    Function.prototype.myCall = function (oThis) {
        let context = oThis || window
        context.fn = this // this指向函数 getValue
        let args = [...arguments].slice(1) // ["yck", "24"]
        const result = context.fn(...args)
        delete context.fn
        return result
    }

    Function.prototype.myApply = function (oThis) {
        let context = oThis || window
        context.fn = this // this指向函数 getValue
        let result;
        if (arguments[1]) {
            result = context.fn(...arguments[1])
        } else {
            result = context.fn()
        }
        delete context.fn
        return result
    }

    Function.prototype.myBind = function (context) {
        if (typeof this !== 'function') {
            throw new TypeError('Error')
        }
        let _this = this
        let args = [...arguments].slice(1)
        return function F() {
            // 判断是否被当做构造函数使用
            if (this instanceof F) {
                return _this.apply(this, args.concat([...arguments]))
            }
            return _this.apply(context, args.concat([...arguments]))
        }
    }

    // 区别：接收的参数不同

    // 测试
    let a = {
        value: 1
    }
    function getValue(name, age) {
        console.log(name)
        console.log(age)
        console.log(this.value)
    }
    getValue.myCall(a, 'yck', '24')
    getValue.myApply(a, ['yck', '24'])

    // getValue.call(a, 'yck', '24')
    // getValue.apply(a, ['yck', '24'])

</script>